/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import java.util.List;
import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import pojo.Chuongtrinhkhuyenmai;
import util.HibernateUtil;

/**
 *
 * @author lhaquang
 */
public class khuyenmaiDAO {

    static Logger log = Logger.getLogger(khuyenmaiDAO.class.getName());

    /**
     * Get all Chuong Trinh Khuyen Mai
     * @return 
     */
    public static List<Chuongtrinhkhuyenmai> layDSChuongTrinhKhuyenMai() {
        List<Chuongtrinhkhuyenmai> ds = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        log.info("Get session successful.");
        try {
            String hql = "select chkm from Chuongtrinhkhuyenmai chkm";
            log.info("Query SQL string: " + hql);
            Query query = session.createQuery(hql);
            ds = query.list();
            log.info("Data length: " + ds.size());
        } catch (HibernateException ex) {
            log.error(ex);
        } finally {
            session.close();
            log.info("Close session successful");
        }
        return ds;
    }
    
    /**
     * Lay thong tin chuong trinh khuyen mai dua vao Ma CTKM
     * @return 
     */
    public static Chuongtrinhkhuyenmai layThongTinChuongTrinhKhuyenMai(int maChuongTrinhKhuyenMai) {
        Chuongtrinhkhuyenmai ctkm = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        log.info("Get session successful.");
        try {
            ctkm = (Chuongtrinhkhuyenmai)session.get(Chuongtrinhkhuyenmai.class, maChuongTrinhKhuyenMai);            
        } catch (HibernateException ex) {
            log.error(ex);
        } finally {
            session.close();
            log.info("Close session successful");
        }
        return ctkm;
    }
    
    /**
     * them chuong trinh khuyen mai
     * @param chuongTrinhKhuyenMai
     * @return 
     */
    public static boolean themChuongTrinhKhuyenMai(Chuongtrinhkhuyenmai chuongTrinhKhuyenMai) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        log.info("Get session successful.");
        Transaction transaction = null;
        try {
            if(layThongTinChuongTrinhKhuyenMai(chuongTrinhKhuyenMai.getMakhuyenmai()) != null){
                log.info(chuongTrinhKhuyenMai.getMakhuyenmai() + " da ton ton.");
                return false;
            }
            transaction = session.beginTransaction();
            session.save(chuongTrinhKhuyenMai);
            transaction.commit();
            log.info("Save data successful: " + chuongTrinhKhuyenMai.getMakhuyenmai());
        } catch (HibernateException ex) {
            log.error(ex);
        } finally {
            session.close();
            log.info("Close session successful");
        }
        return true;
    }
    
    /**
     * Cap nhat chuong trinh khuyen mai
     * @param chuongTrinhKhuyenMai
     * @return 
     */
     public static boolean capNhatChuongTrinhKhuyenMai(Chuongtrinhkhuyenmai chuongTrinhKhuyenMai) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        log.info("Get session successful.");
        Transaction transaction = null;
        try {
            if(layThongTinChuongTrinhKhuyenMai(chuongTrinhKhuyenMai.getMakhuyenmai()) == null){
                log.info(chuongTrinhKhuyenMai.getMakhuyenmai() + " khong ton tai.");
                return false;
            }
            transaction = session.beginTransaction();
            session.update(chuongTrinhKhuyenMai);
            transaction.commit();
            log.info("Update data successful: " + chuongTrinhKhuyenMai.getMakhuyenmai());
        } catch (HibernateException ex) {
            log.error(ex);
        } finally {
            session.close();
            log.info("Close session successful");
        }
        return true;
    }
     
     /**
      * Xoa chuong trinh khuyen mai
      * @param chuongTrinhKhuyenMai
      * @return 
      */
     public static boolean xoaChuongTrinhKhuyenMai(Chuongtrinhkhuyenmai chuongTrinhKhuyenMai) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        log.info("Get session successful.");
        Transaction transaction = null;
        try {
            if(layThongTinChuongTrinhKhuyenMai(chuongTrinhKhuyenMai.getMakhuyenmai()) == null){
                log.info(chuongTrinhKhuyenMai.getMakhuyenmai() + " khong ton tai.");
                return false;
            }
            transaction = session.beginTransaction();
            session.delete(chuongTrinhKhuyenMai);
            transaction.commit();
            log.info("Delete data successful: " + chuongTrinhKhuyenMai.getMakhuyenmai());
        } catch (HibernateException ex) {
            log.error(ex);
        } finally {
            session.close();
            log.info("Close session successful");
        }
        return true;
    }
     
     /**
      * Xoa chuong trinh khuyen mai
      * @param chuongTrinhKhuyenMai
      * @return 
      */
     public static boolean xoaChuongTrinhKhuyenMai(int maChuongTrinhKhuyenMai) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        log.info("Get session successful.");
        Transaction transaction = null;
        Chuongtrinhkhuyenmai ctkm = null;
        try {
            if((ctkm = layThongTinChuongTrinhKhuyenMai(maChuongTrinhKhuyenMai)) == null){
                log.info(maChuongTrinhKhuyenMai + " khong ton tai.");
                return false;
            }
            transaction = session.beginTransaction();
            session.delete(ctkm);
            transaction.commit();
            log.info("Delete data successful: " + ctkm.getMakhuyenmai());
        } catch (HibernateException ex) {
            log.error(ex);
        } finally {
            session.close();
            log.info("Close session successful");
        }
        return true;
    }
}
